<?php
/**
 * @author ANHTAM
 */
// Require module for controller

class Category_Controller extends ControllerAdmin {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->menuDanhMuc = true;
    	$this->menuCate = true;
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = '';
        $this->tpl_title_list = '';
        $this->arrContent = array();
		$this->currPage = '';
		$this->numberPage = '';
		$this->strPaging = '';
        
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        $dataGet = FunctionCommon::getGet(array("danhmucid"));
        $danhmucid = $dataGet['danhmucid'];
        if ($danhmucid != ''){
        	Session::Set("danhmucid", $danhmucid);
        } else {
        	$danhmucid = Session::Get("danhmucid");
        }

        $ojbCategory = new SanphamCategory();
        if ($this->isPost()) {
        	$dataPost = FunctionCommon::getPost(array('itemID', 'mode', 'type', 'checkdel'));
	        // Delete by id
	        if ($dataPost['mode'] == 'delete') {
	        	$dataSanpham = $ojbCategory->getByPrimaryKey($dataPost['itemID']);
	        	$ojbCategory->delete(array('id' => $dataPost['itemID']));
	        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataSanpham['code']);
			    FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataSanpham['code']);
	        }
	        
	        // Delete all by array id
	    	if ($dataPost['mode'] == 'deleteall') {
	    		$arrPost = $dataPost['checkdel'];
	    		if (count ($arrPost) > 0) {
	    			foreach ($arrPost as $aP) {
	    				$dataSanpham = $ojbCategory->getByPrimaryKey($aP);
	        			$ojbCategory->delete(array('id' => $aP));
	        			FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataSanpham['code']);
			    		FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataSanpham['code']);
	    			}
	    		}
	        }
	        
	        // Up
	        if ($dataPost['mode'] == 'up') {
	        	$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	$seqMin = $ojbCategory->getMinMax(array("seq"=>array("math"=>">", "value"=>$seq), "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid)));
	        	$idMin = $ojbCategory->getIdBySeq($seqMin);
	        	
	        	$ojbCategory->setAttribute('seq', $seqMin);
	        	$ojbCategory->update(array('id' => $id));
	        	$ojbCategory->emptyAttribute();
	        	$ojbCategory->setAttribute('seq', $seq);
	        	$ojbCategory->update(array('id' => $idMin));
	        }
	        
	    	// Down
	        if ($dataPost['mode'] == 'down') {
	        	$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	$seqMax = $ojbCategory->getMinMax(array("seq"=>array("math"=>"<", "value"=>$seq), "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid)), true);
	        	$idMax = $ojbCategory->getIdBySeq($seqMax);
	        	
	        	$ojbCategory->setAttribute('seq', $seqMax);
	        	$ojbCategory->update(array('id' => $id));
	        	$ojbCategory->emptyAttribute();
	        	$ojbCategory->setAttribute('seq', $seq);
	        	$ojbCategory->update(array('id' => $idMax));
	        }
        }
        
        $this->maxseq = $ojbCategory->getMinMax(array('danhmuc_id'=>array('math'=>'=', 'value'=>$danhmucid)), true);
        $this->minseq = $ojbCategory->getMinMax(array('danhmuc_id'=>array('math'=>'=', 'value'=>$danhmucid)));
        
        // List all
    	$arrParam = array('m' => 'category');
    	$arrField = array('id', 'seq', 'danhmuc_id', 'name', 'code');
        $condition = array('danhmuc_id'=>array('math'=>'=', 'value'=>$danhmucid));
		
        $data = FunctionCommon::paging($ojbCategory, $arrParam, $arrField, $condition);//$this->paging($arrParam, $arrField, $condition);
        $arrContent = $data["data"];
		if (count ($arrContent) > 0) {
			foreach ($arrContent as $content) {
				$this->arrContent[] = $content;
			}
			$this->currPage = $data["currPage"];
			$this->numberPage = $data["numberPage"];
			$this->strPaging = $data["strPaging"];
		}

        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
	/**
     * actionEdit
     * - page edit of controller
     */
    public function actionEdit() {
    	
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = '';
        $this->tpl_button = "Thêm mới";
        $this->tpl_title_list = '';
        $this->dialog = false;
        $this->id = '';
        $this->tenFile = '';
        
    	if (!$this->loginFlag || Session::Get("danhmucid") == '' || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        $this->dataPost = array('itemID'=>'', 'mode'=>'', 'type'=>'', 'name'=>'', 'nameimage'=>'');
		$danhmucid = Session::Get("danhmucid");
        
        $ojbCategory = new SanphamCategory();
        if ($this->isPost()) {
        	$this->dataPost = FunctionCommon::getPost(array_keys($this->dataPost));
        	$this->id = $this->dataPost['itemID'];
        	
        	if ($this->dataPost['mode'] != '') {
        		// Update database
        		if ($this->dataPost['type'] == 'edit' && $this->id != '') {
        			$ojbCategory->setAttribute('name', $this->dataPost['name']);
	        		$ojbCategory->setAttribute('code', $this->dataPost['nameimage']);
	        		$ojbCategory->update(array('id' => $this->id));
	        		$this->dialog = true;
        		}
        		
        		// Insert Database
        		if ($this->dataPost['type'] == 'edit' && $this->id == '') {
        			$ojbCategory->setAttribute('danhmuc_id', $danhmucid);
        			$ojbCategory->setAttribute('seq', $ojbCategory->getMinMax(array(), true) + 1);
		        	$ojbCategory->setAttribute('name', $this->dataPost['name']);
		        	$ojbCategory->setAttribute('code', $this->dataPost['nameimage']);
		        	$ojbCategory->setAttribute('insert_at', date('Y-m-d H:i:s'));
		        	$ojbCategory->insert();
		        	$this->dialog = true;
		        	//exit;
        		}
        		
        		// Xu ly file
	        	if ($this->dataPost['type'] == 'upload') {
	        		$this->tenFile = FunctionCommon::uploadFileTo($_FILES["code"], "img-the", $this->dataPost['nameimage']);
	        		if ($this->tenFile == false) {
	        			$this->tenFile = $this->dataPost['nameimage'];
	        		}
		        }
		        
		        // Delete image
	        	if ($this->dataPost['type'] == 'delete') {
		        	$tenFile = $this->dataPost['nameimage'];
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$tenFile);
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$tenFile);
		        }
        	} else {
        		$this->tpl_button = "Thay đổi";
	        	$this->dataPost = $ojbCategory->getByPrimaryKey($this->id);
				$this->tenFile = $this->dataPost['code'];
        	}
        }
        
        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
}
?>

